package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.vo.UpdateOrderBoneVoRequest;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.OrderService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zd on 2017/4/21.
 * 这个类没用了，可以删了
 */
@Service("orderboneextendServiceImpl")
public class OrderBoneExtendServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(OrderBoneExtendServiceImpl.class);

    public ResponseObject updateOrderboneextend(String data) {
        ResponseObject responseObject = new ResponseObject();
        try {
            UpdateOrderBoneVoRequest request = JsonUtils.parseObject(data, UpdateOrderBoneVoRequest.class);
            if (StringUtils.isBlank(request.getOrderNo())){
                responseObject.setResultCodeAndMessage("1001","请求参数order_no 缺失");
                return responseObject;
            }

            Map<String, String> params = new HashMap<>();
            OrderExtVo order = null;
            params.put("order_no",request.getOrderNo());
            logger.info("仁医==>查询订单开始 order_no ={}",new Object[]{request.getOrderNo()});
            Response response = BaseService.callHumpFromInternal("yyt.base.order.by.orderno.get", JsonUtils.toJsonString(params));
            order = JsonUtils.parseObject(response.getResult(), OrderExtVo.class);
            if(!"0".equals(response.getResultCode()) || null==order){
                logger.info("仁医==>查询订单失败 :"+response.getResultMessage());
                responseObject.setResultCodeAndMessage("1998","查询订单失败");
                return responseObject;
            }
            logger.info("仁医==>查询订单结束 返回 order ={}",new Object[]{JsonUtils.toJsonString(order)});
            if (StringUtils.isNotBlank(request.getUserName())||StringUtils.isNotBlank(request.getMobile())||
                    StringUtils.isNotBlank(request.getBirth())||StringUtils.isNotBlank(request.getSex())){

                try {
                    logger.info("仁医==>更新患者信息开始 user_id={}",new Object[]{order.getUserId()} );
                    Map<String, String> updataPersonalUserParams = new HashMap<>();
                    updataPersonalUserParams.put("user_id",order.getUserId());
                    updataPersonalUserParams.put("user_name",request.getUserName());
                    updataPersonalUserParams.put("mobile",request.getMobile());
                    updataPersonalUserParams.put("birth",request.getBirth());
                    updataPersonalUserParams.put("sex",request.getSex());
                    response = BaseService.callHumpFromInternal("yyt.base.personaluser.update", JsonUtils.toJsonString(updataPersonalUserParams));
                    if (!"0".equals(response.getResultCode())){
                        responseObject.setResultCodeAndMessage("1998","更新患者信息失败");
                        return responseObject;
                    }

                }catch (Exception e){
                    logger.info("系统异常"+e.getCause());
                    responseObject.setResultCodeAndMessage("1999","更新患者信息失败");
                    return responseObject;
                }
            }

            if (StringUtils.isNotBlank(request.getDiseaseDesc())||
                StringUtils.isNotBlank(request.getDiseaseName())||
                StringUtils.isNotBlank(request.getDiseasePicUrls())||
                StringUtils.isNotBlank(request.getAppointmentDoctorName())||
                StringUtils.isNotBlank(request.getAppointmentDoctorId())||
                StringUtils.isNotBlank(request.getUploadMethod())){
                try {
                    Map<String, String> updateOrderBoneExtendParams = new HashMap<>();
                    updateOrderBoneExtendParams.put("order_no",request.getOrderNo());
                    updateOrderBoneExtendParams.put("disease_desc",request.getDiseaseDesc());
                    updateOrderBoneExtendParams.put("disease_name",request.getDiseaseName());
                    updateOrderBoneExtendParams.put("disease_pic_urls",request.getDiseasePicUrls());
                    updateOrderBoneExtendParams.put("appointment_doctor_name",request.getAppointmentDoctorName());
                    updateOrderBoneExtendParams.put("appointment_doctor_id",request.getAppointmentDoctorId());
                    updateOrderBoneExtendParams.put("upload_method",request.getUploadMethod());
                    logger.info("仁医==>更新骨科拓展订单开始 order_no ={}",new Object[]{request.getOrderNo()});
                    response = OrderService.callHumpFromInternal("yyt.order.bone.extend.update", JsonUtils.toJsonString(updateOrderBoneExtendParams));
                    if(!"0".equals(response.getResultCode())){
                        logger.info("仁医==>更新骨科拓展订单 :"+response.getResultMessage());
                        responseObject.setResultCodeAndMessage("1998","更新骨科拓展订单");
                        return responseObject;
                    }
                }catch (Exception e){
                    logger.info("系统异常"+e.getCause());
                    responseObject.setResultCodeAndMessage("1999","更新订单信息失败");
                    return responseObject;
                }
            }
        }catch (Exception e){
            logger.error("系统异常",e);
            responseObject.setResultCodeAndMessage("1999","系统异常请联系管理员");
        }
        responseObject.setResultCodeAndMessage("0","成功");
        return responseObject;
    }

}
